package com.demo.mqtt.zigbee.handeler;

import com.demo.mqtt.common.client.MqttConnectionUtils;
import com.demo.mqtt.common.constants.TopictConstants;
import com.demo.mqtt.common.utils.AssistUtil;
import com.demo.mqtt.common.utils.HexConvertUtil;
import com.demo.mqtt.zigbee.handeler.upload.FeaturesDataUpload;
import com.demo.mqtt.zigbee.handeler.upload.HeartBeatUpload;
import com.demo.mqtt.zigbee.handeler.upload.WavesDataUpload;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: mqtt
 * @description: 信息处理器
 * @author: liuYunKai
 * @create: 2021-03-24 15:00
 */
@Component
@Slf4j
public class Handeler implements TopictConstants {

    @Autowired
    private HeartBeatUpload heartBeatUpload;
    @Autowired
    private FeaturesDataUpload featuresDataUpload;
    @Autowired
    private WavesDataUpload wavesDataUpload;
    @Autowired
    private MqttConnectionUtils mqttConnectionUtils;

    public boolean payloadHandel(String topic, MqttMessage mqttMessage) {

        int messageId = mqttMessage.getId();

        String pqyload = new String(mqttMessage.getPayload());

        if (StringUtils.isNotBlank(pqyload)) {

            //转为hex
            String hesStr = HexConvertUtil.bytesToHexString(mqttMessage.getPayload());
            log.info("hesStr : {}", hesStr);

            //按字节解析
            ByteBuf byteBuf = Unpooled.buffer();
            byteBuf.writeBytes(mqttMessage.getPayload());
            //协议头
            String header = AssistUtil.readByteBuf(byteBuf, 2);
            //协议版本号
            String version = AssistUtil.readByteBuf(byteBuf, 1);
            //设备地址(BCD码格式)
            String addr = AssistUtil.readByteBuf(byteBuf, 6);
            //序列号
            String sn = AssistUtil.readByteBuf(byteBuf, 2);
            //通讯命令
            String command = AssistUtil.readByteBuf(byteBuf, 1);
            //数据长度(小端模式)
            String length = AssistUtil.readByteBuf(byteBuf, 4);

            boolean needAck = false;
            boolean isSuccess = false;
            switch (topic) {
                case PUB_FEATURES_DATAS:
                    isSuccess = featuresDataUpload.annalyze(topic, hesStr, command,mqttMessage);
                    needAck = true;
                    break;
                case PUB_WAVES_DATAS:
                    needAck = true;
                    isSuccess = wavesDataUpload.annalyze(topic, hesStr, command);
                    break;
                case PUB_OBJ_PARAM_ACK:
                    break;
                case PUB_GW_PARAM_ACK:
                    break;
                case PUB_NORMAL_ACK:
                    break;
                case PUB_HEART_BEAT:
                    heartBeatUpload.annalyze(topic, hesStr, command);
                    break;
                default:
                    log.error("topic mach fail, topic value => [{}]", topic);
            }

            if (needAck) {
                pubNormalAck(hesStr, sn, command, isSuccess);
            }


        }


        return false;
    }

    private void pubNormalAck(String hesStr, String sn, String command, boolean isSuccess) {

        String resultStr = isSuccess ? "00" : "ff";

        String msg = hesStr.substring(0, 22) + "0104000000"+sn + command + resultStr;

        try {
            mqttConnectionUtils.pubToNormalAck(msg,1 );
        } catch (MqttException e) {
           log.error("erro message:{}",e.getMessage());
        }

    }

    /**
     * 获取推送类型
     *
     * @param hexStr
     * @return
     */
    String getPushType(String hexStr) {
        return hexStr.substring(0, 2);
    }
}
